package com.lms.controller;

import com.lms.bean.Book;
import com.lms.bean.RentRecord;
import com.lms.bean.RentRecordQuery;
import com.lms.bean.User;
import com.lms.service.BookService;
import com.lms.service.RentRecordService;
import com.lms.service.UserService;
import com.lms.util.MyBeanUtils;
import com.lms.util.QueryResult;
import com.lms.util.ResultUtils;
import com.lms.vo.BookVO;
import com.lms.vo.RentRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("admin/rentRecord")
public class RentRecordController {
    @Autowired
    RentRecordService rentRecordService;

    @Autowired
    UserService userService;

    @Autowired
    BookService bookService;

    @RequestMapping("rentRecord-list")
    public String toRentRecordList() {
        return "admin/rentRecord-list";
    }

    @RequestMapping("rentRecord-input")
    public String toRentRecordInput(Long rentId,Model model) {
        if(rentId==-1){//添加
            RentRecord rentRecord=new RentRecord();
            rentRecord.setRentId(new Long(-1));
            model.addAttribute("rentRecord",rentRecord);
        }else{//编辑
            RentRecord rentRecord=rentRecordService.findById(rentId);
            model.addAttribute("rentRecord",rentRecord);
        }
        List<Boolean> states=new ArrayList<>();
        states.add(Boolean.TRUE);
        states.add(Boolean.FALSE);
        model.addAttribute("states",states);
        return "admin/rentRecord-input";
    }

 /*   @RequestMapping("all")
    @ResponseBody
    public ResultUtils<RentRecordVO> findAll(@RequestParam("page") Integer pageFromLayUI,
                                             @RequestParam("limit") Integer limitFromLayUI,
                                             @RequestParam(value ="searchName",defaultValue = "")  String rentId) {
        //获取记录的总数，交给前端计算分页
        Long count;
        List<RentRecordVO> data ;
        if(!StringUtils.isEmpty(rentId)&&rentId!=null){
            count = rentRecordService.getTotalCount(Long.valueOf(rentId));
            //获取当前页的记录列表交给前端渲染,注意 layui 和 pageable 关于 page 的计数差别
            data= rentRecordService.findAll(pageFromLayUI - 1, limitFromLayUI,Long.valueOf(rentId));

        }else{
            count = rentRecordService.getTotalCount();
            data = rentRecordService.findAll(pageFromLayUI - 1, limitFromLayUI);
        }

        //返回JSON结果
        return ResultUtils.layUITable(count, data);
    }*/
 @RequestMapping("all")
 @ResponseBody
 public ResultUtils<RentRecordVO> findAll(@RequestParam("page") Integer pageFromLayUI,
                                          @RequestParam("limit") Integer limitFromLayUI,
                                          @RequestParam(value = "username",defaultValue = "")  String username,
                                          @RequestParam(value = "bookname",defaultValue = "")  String bookname,
                                          @RequestParam(value = "state",defaultValue = "") Boolean state) {
     //获取记录的总数，交给前端计算分页
     List<RentRecordVO> data ;
     //封装前端查询参数
     RentRecordQuery param = new RentRecordQuery();
     param.setBookname(bookname);
     param.setUsername(username);
     param.setState(state);
     //复合查询
     QueryResult<RentRecordVO> queryResult = rentRecordService.queryByBookNameLikeOrUsernameLikeOrState(pageFromLayUI - 1, limitFromLayUI,param);
     //返回JSON结果
     return ResultUtils.layUITable(queryResult.getTotalCount(), queryResult.getData());

 }

    @RequestMapping("addRentRecord")
    public String save(RentRecord rentRecord){
     if(rentRecord.getExpectReturnTime()==null){//如果没有设置预期归还时间，默认预期归还时间是一个月后
        Calendar cal = Calendar.getInstance();
        cal.setTime(rentRecord.getRentTime());   //设置当前时间
        cal.add(Calendar.MONTH, 1);
        rentRecord.setExpectReturnTime(cal.getTime());
     }
     if(rentRecord.getRentTime()==null){//编辑的时候设置了时间控件为null，所以需要重新赋值
        RentRecord n=rentRecordService.findById(rentRecord.getRentId());
        rentRecord.setRentTime(n.getRentTime());
     }
        rentRecordService.save(rentRecord);
        return "redirect:/admin/rentRecord/rentRecord-list";//绝对路径
    }
    @RequestMapping("deleteRentRecord")
    public String delete(Long rentId){
        rentRecordService.delete(rentId);

        return "redirect:/admin/rentRecord/rentRecord-list";
    }

    @RequestMapping("deleteChecked")
    public String deleteChecked(String rentRecordList){
        String[] rentRecords = rentRecordList.split(",");
        List<Long> ids = new ArrayList<>();
        for (String s : rentRecords){
            ids.add(Long.valueOf(s));
        }
        rentRecordService.deleteAll(ids);
        return "redirect:/admin/rentRecord/rentRecord-list";
    }
}
